// Copyright 2024 Tvux
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _125

import "strings"

// isAlNum 判断给定字符是否是小写字母或数字
func isAlNum(ch byte) bool {
	return ('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9')
}

// isPalindrome
// 时间优化不了了，考虑优化空间，直接在本地判断，使用双指针，判断左右两边每个字母数字字符是否相等
func isPalindrome(s string) bool {
	s = strings.ToLower(s)
	left, right := 0, len(s)-1
	for left < right {
		// 先从左边找到第一个字母数字字符
		for left < right && !isAlNum(s[left]) {
			left++
		}
		// 接着从右边找到第一个字母数字字符
		for left < right && !isAlNum(s[right]) {
			right--
		}
		// 然后比较
		if left < right {
			if s[left] != s[right] {
				return false
			}
			left++
			right--
		}
	}
	return true
}

// isPalindrome
// 时间复杂度：O(n)
// 空间复杂度：O(n)
//func isPalindrome(s string) bool {
//	s = strings.ToLower(s)
//	input := make([]rune, 0, len(s))
//	for i := range s {
//		if ('a' <= rune(s[i]) && rune(s[i]) <= 'z') || ('0' <= rune(s[i]) && rune(s[i]) <= '9') {
//			input = append(input, rune(s[i]))
//		}
//	}
//
//	for i, j := 0, len(input)-1; i < j; i, j = i+1, j-1 {
//		if input[i] != input[j] {
//			return false
//		}
//	}
//	return true
//}
